package com.qf.index;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class d5 {
    /**
     * 这是使用阿里的通义，使用4个账号，区分方法是把自增主键的后两位分为4段，0-24；25-49；50-74；75-99，均分一起跑。
     * 贾新志 2024.12.28
     * Bearer sk-c65ee8baf0c440238e51f74495107da3 公司[1]
     * Bearer sk-68b83bd45df542ada3d7882e116f63f0 JXZ[2]
     * Bearer sk-78636476472d45a1835795ca82c3d7d6 GCR[3]
     * Bearer sk-06a0675779c944b6a4d5b893cb3cc89a ZFL[4]
     */
    public static void main(String[] args) throws SQLException {
        String user = "Bearer sk-06a0675779c944b6a4d5b893cb3cc89a";
        String newDataName = "scholar_25_02.";
        String ncTextTable = "html.nc_text_table";
        String ncTextGuidelineTable = "html.nc_text_table_guideline";
        String adInfoSumTable = newDataName + "ad_info_sum";
        String adInfoYnTable = newDataName + "ad_info_sum_2";
        String summaryTable = newDataName + "unified_search_summary_0120";
        String summaryOldTable = newDataName + "unified_search_summary_0120_old";


//        Map<Integer, String> aidAndLanguageMap = getAidAndLanguageMap(summaryOldTable, "id", "aid`,`language");
//        System.out.println("aidAndLanguageMap.size():" + aidAndLanguageMap.size());
//        updateSummaryTable(summaryTable, "id", "aid", aidAndLanguageMap);

        updateC1Auth(summaryTable, "id", "rp_au");
    }

    public static void updateC1Auth(String summaryTable, String autoId, String field) throws SQLException {
        List<Entity> tableNum = Db.use().query("select count(1) as count from " + summaryTable);
        String limit = "";
//        String limit = " limit 1";
        int onePageNum = 1000;
        if (tableNum.get(0).getInt("count") > 0) {
            int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + summaryTable).intValue();
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            tableList.parallelStream().forEach(startId -> {
                try {
                    List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + summaryTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1) + limit);
                    for (Entity entry : tableData) {
                        Integer id = entry.getInt(autoId);
                        String rpAu = entry.getStr("rp_au");
                        if (null != id && id > 0 && null != rpAu && rpAu.contains("sort")) {
                            List<En.C1Auth> rpList = JSONObject.parseArray(rpAu, En.C1Auth.class);
                            List<En.C1Auth> resultList = new ArrayList<>();
                            for (int i = 0; i < rpList.size(); i++) {
                                En.C1Auth rC1Auth = new En.C1Auth();
                                En.C1Auth c1Auth = rpList.get(i);
                                rC1Auth.setAuthList(c1Auth.getAuthList());
                                rC1Auth.setUnitSet(c1Auth.getUnitSet());
                                rC1Auth.setStdUnit(c1Auth.getStdUnit());
                                rC1Auth.setCountry(c1Auth.getCountry());
                                rC1Auth.setEduSet(c1Auth.getEduSet());
                                rC1Auth.setInfo(c1Auth.getInfo());
                                rC1Auth.setFull(c1Auth.getFull());
                                rC1Auth.setDeptSet(c1Auth.getDeptSet());
                                rC1Auth.setStemSet(c1Auth.getStemSet());
                                rC1Auth.setFuDanSet(c1Auth.getFuDanSet());
                                rC1Auth.setSort(i + 1);
                                resultList.add(rC1Auth);
                            }
                            try {
                                Db.use().update(Entity.create().set("rp_au", JSONUtil.toJsonStr(resultList)), Entity.create(summaryTable).set(autoId, id));
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            });
        }
    }

    public static void updateSummaryTable(String summaryTable, String autoId, String field, Map<Integer, String> aidAndLanguageMap) throws SQLException {
        List<Entity> tableNum = Db.use().query("select count(1) as count from " + summaryTable);
        String limit = "";
//        String limit = " limit 1";
        int onePageNum = 1000;
        Map<String, String> languageMap = uSet.getLanguageMap();
        if (tableNum.get(0).getInt("count") > 0) {
            int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + summaryTable).intValue();
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            tableList.parallelStream().forEach(startId -> {
                try {
                    List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + summaryTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1) + limit);
                    for (Entity entry : tableData) {
                        Integer aid = entry.getInt("aid");
                        Integer id = entry.getInt(autoId);
                        if (null != aid && aidAndLanguageMap.containsKey(aid) && null != id && id > 0) {
                            String language = aidAndLanguageMap.get(aid);
                            String[] split = language.replace(";-;", "ĺļ").replace("; ", "ĺļ").replace(";", "ĺļ").split("ĺļ");
                            List<String> languageList = new ArrayList<>();
                            for (String s : split) {
                                if (s.trim().length() > 2) {
                                    languageList.add(languageMap.getOrDefault(s.toLowerCase().trim(), s.trim()));
                                }
                            }
                            if (!languageList.isEmpty()) {
                                try {
                                    Db.use().update(Entity.create().set("language", CollUtil.join(languageList, "; ")), Entity.create(summaryTable).set(autoId, id));
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            });
        }
    }

    public static Map<Integer, String> getAidAndLanguageMap(String summaryOldTable, String autoId, String field) throws SQLException {
        List<Entity> tableNum = Db.use().query("select count(1) as count from " + summaryOldTable);
        String limit = "";
//        String limit = " limit 1";
        int onePageNum = 1000;
        Map<Integer, String> aidAndLanguageMap = new ConcurrentHashMap<>();
        if (tableNum.get(0).getInt("count") > 0) {
            int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + summaryOldTable).intValue();
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            tableList.parallelStream().forEach(startId -> {
                try {
                    List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + summaryOldTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1) + limit);
                    for (Entity entry : tableData) {
                        String language = entry.getStr("language");
                        Integer aid = entry.getInt("aid");
                        if (null != aid && aid > 0 && null != language && language.length() > 2) {
                            aidAndLanguageMap.put(aid, language);
                        }
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            });
        }
        return aidAndLanguageMap;
    }


}
